package com.gwz.controller.admin;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.gwz.context.ProContent;
import com.gwz.model.Admin;
import com.gwz.model.JobBean;
import com.gwz.service.admin.QuartzBeanService;


/**
 * 管理quartz控制层（增删改查）
 * @author panguixiang
 *
 */
@Controller
@RequestMapping("/admin/quartz")
public class ManagQuartzController {

    private static Logger     logger = Logger.getLogger(ManagQuartzController.class);

    @Autowired
    private QuartzBeanService quartzBeanService;

    /**
     * 查询job列表
     * @param model
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(Model model) {
        List<JobBean> jobList = quartzBeanService.list();
        model.addAttribute("quartzList", jobList);
        return "/admin/quartzs";
    }

    /**
     * 查询job列表
     * @param model
     * @return
     */
    @RequestMapping(value = "initSave", method = RequestMethod.GET)
    public String initSave(Model model) {
        return "/admin/save";
    }

    /**
     * 新增job
     * @param jobBean
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String add(@ModelAttribute("jobBean") @Valid JobBean jobBean, BindingResult result,
                      HttpSession session) {

        Admin admin = (Admin) session.getAttribute(ProContent.LOGIN_SESSION);
        logger.info("====登录用户======".concat(admin.getUsername()).concat("===执行了  新增 Job操作").concat(",任务Bean=")
            .concat(JSONObject.toJSONString(jobBean)));

        if (result.hasErrors()) {
            return "/admin/save";
        }
        try {
            quartzBeanService.addJob(jobBean);
        } catch (Exception e) {
            logger.error("新增Job异常", e.fillInStackTrace());
        }
        return "redirect:/admin/quartz/list";
    }

    /**
     * 删除job
     * @param id
     * @return
     */
    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET, produces = "application/json")
    public String delete(@PathVariable("id") String id, HttpSession session) {

        Admin admin = (Admin) session.getAttribute(ProContent.LOGIN_SESSION);

        JobBean bean = null;
        try {
            bean = quartzBeanService.getById(id);
            if (bean != null) {
                quartzBeanService.remove(id);
            }
        } catch (Exception e) {
            logger.error("删除Job异常", e.fillInStackTrace());
        } finally {
            if (bean != null) {
                logger.info("====登录用户======".concat(admin.getUsername()).concat("===执行了  删除 Job操作")
                    .concat(",任务Bean=").concat(JSONObject.toJSONString(bean)));
            }
        }
        return "redirect:/admin/quartz/list";
    }
    
}
